using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PluginHandler.Database;

namespace NpgsqlPlugin.Database
{
    public class DatabaseTableColumn : IDatabaseTableColumn
    {
        private DatabaseHandler _databaseHandler;
        private IDatabaseObject _parentDatabaseObject;
        private DbType _dbType;
        private string _name;
        private string _schemaType;

        internal DatabaseTableColumn(DatabaseHandler databaseHandler, IDatabaseObject parentDatabaseObject, string name, DbType dbType, string schemaType)
        {
            _databaseHandler = databaseHandler;
            _parentDatabaseObject = parentDatabaseObject;
            _name = name;
            _dbType = dbType;
            _schemaType = schemaType;
        }

        internal static DbType ConvertStringToDbType(string schemaType)
        {
            switch (schemaType.ToLower())
            { 
                case "varchar":             return DbType.AnsiString;
                case "char":                return DbType.AnsiStringFixedLength;
                case "nvarchar":            return DbType.String;
                case "nchar":               return DbType.StringFixedLength;
                case "datetime":            return DbType.DateTime;
                case "int4":                return DbType.Int32;
                case "aclitem":             return DbType.Guid;
                case "anyarray":            return DbType.Object;
                case "image":               return DbType.Binary;
                case "bytea":               return DbType.Byte;
                case "float4":              return DbType.Double;
                case "inet":                return DbType.Object;
                case "int2vector":          return DbType.Object;
                case "int2":                return DbType.Int16;
                case "int8":                return DbType.Int64;
                case "abstime":             return DbType.DateTime;
                case "bool":                return DbType.Boolean;
                case "float":               return DbType.Double;
                case "interval":            return DbType.Currency;
                case "name":                return DbType.VarNumeric;
                case "oid":                 return DbType.Double;
                case "oidvector":           return DbType.Date;
                case "regproc":             return DbType.Currency;
                case "tid":                 return DbType.Object;
                case "text":                return DbType.AnsiString;
                case "_text":               return DbType.AnsiString;
                case "_varchar":            return DbType.AnsiString;
                case "_regtype":            return DbType.Object;
                case "_oid":                return DbType.Object;
                case "_int2":               return DbType.Object;
                case "_float4":             return DbType.Object;
                case "_char":               return DbType.Object;
                case "_aclitem":            return DbType.Object;
                case "unknown":             return DbType.Object;
                case "timestamptz":         return DbType.DateTime;
                case "xid":                 return DbType.Xml;
                default:                    throw new NotImplementedException(
                                                "Datatype '" + 
                                                schemaType + 
                                                "' cannot be converted to enum!"); //return DbType.Object;
            }
        }

        #region IDatabaseTableColumn Member

        public string Name
        {
            get 
            { 
                return _name; 
            }
        }

        public DbType DbType
        {
            get 
            { 
                return _dbType; 
            }
        }

        public string NativeDbTypeName
        {
            get 
            {
                return _schemaType; 
            }
        }

        #endregion
    }
}
